package uit.uthuoc.fragment;

import java.util.ArrayList;

import uit.uthuoc.dal.DrugDAO;
import uit.uthuoc.dal.DrugDTO;
import uit.uthuoc.dal.MIMSClassDTO;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.uit.uthuoc.R;

public class SearchResultFragment extends ListFragment {
	
	private static final String LOG_ID = "uit.uthuoc.fragment.SearchResultFragment";

    private SearchView mSearchView;
    private String mSearchString;	// use this for highlighting
	private ArrayList<DrugDTO> mDrugList;
	private SearchResultFragmentInterface parentActivity;
	private DrugSearchResultAdapter adapter;
	private final OnQueryTextListener queryListener = new OnQueryTextListener() {

		@Override
		public boolean onQueryTextChange(String arg0) {
			if (arg0.length() > 2) {
				onSearchButtonClicked(arg0);
			} else {
				adapter.clear();
			}
			return true;
		}

		@Override
		public boolean onQueryTextSubmit(String arg0) {
//			onSearchButtonClicked(arg0);
			mSearchView.clearFocus();
			return false;
		}
		
	};
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		DrugDAO.setContext(getActivity());
		adapter = new DrugSearchResultAdapter();
		adapter.clear();		
		setListAdapter(adapter);
	}
	
	

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
	    // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            parentActivity = (SearchResultFragmentInterface) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement SearchResultFragmentInterface");
        }

	}

	
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);
	}



	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.main_activity_actions, menu);
        
        MenuItem searchItem = menu.findItem(R.id.action_search);
        
        mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setQueryHint(getString(R.string.search_hint));
        mSearchView.setOnQueryTextListener(queryListener);
        
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	public void onSearchButtonClicked(String searchText)
	{
		// Clear the current adapter
		adapter.clear();
		mDrugList = DrugDAO.getDAO().searchDrugByDrugName(searchText);
		mSearchString = searchText;	// use this for highlighting
	//	adapter = new ArrayAdapter<DrugDTO>(getActivity(), R.layout.simple_list_item,(ArrayList<DrugDTO>) mDrugList.clone());
	//	setListAdapter(adapter);
		
		for (DrugDTO drugDTO : mDrugList) {
			adapter.add(drugDTO);
		}
		
		setListAdapter(adapter);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		parentActivity.onListItemClickListener(position);
	}
	
	private class DrugSearchResultAdapter extends ArrayAdapter<DrugDTO> {
		public DrugSearchResultAdapter() {
			super(getActivity(), 0);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			DrugDTO item = getItem(position);
			
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(R.layout.search_result_layout, null);
			}
			
			TextView tvDrugName = (TextView) convertView.findViewById(R.id.search_result_tvDrugName);
			TextView tvMIMSCLass = (TextView) convertView.findViewById(R.id.search_result_tvMIMSClass);
			
			String highlightedStr = item.getDrugName().replaceAll(mSearchString, "<font color=\'blue\'>" + mSearchString + "</font>");
			tvDrugName.setText(Html.fromHtml(highlightedStr));
			tvMIMSCLass.setText(item.getMIMSClass().getMIMSClassName());
			
			
			
			return convertView;
		}
		
		
	}
}
